Stylebits Corner - Listboxes

Reviewing the Stylebits Parameters

The four parameters of stylebits are AddBit, RemoveBit, AddExtendedBit, RemoveExtendedBit. For a review of these four parameters, and an introduction to Stylebits in general, please read [Stylebits Corner - An Introduction]. For examples of stylebits changing the appearance and input of textboxes, see [Stylebits Corner - Textboxes]. To see Statictext changes with stylebits, see [Stylebits Corner - Statictext]. To see Button changes with stylebits, see [Stylebits Corner - Buttons].

Stylebits, Listboxes and Borders

The native listbox is black text against a white background. To change the background color to match the default color of the user's scheme, include

BackgroundColor$ = "Buttonface"

prior to defining that listbox. Change the BackgroundColor$ to white (or any other valid color) for subsequent listboxes if desired. The default border is single edged. Techniques to change the border of a listbox are similar to changing the border of a window, textbox or any other control.

Borderless

BackgroundColor$ = "Buttonface"

Stylebits #style.lb1, 0, _WS_BORDER, 0, _WS_EX_CLIENTEDGE

Sunken Border

Stylebits #style.lb2, 0, 0, _WS_EX_CLIENTEDGE or _WS_EX_STATICEDGE, 0

Raised Border

Stylebits #style.lb2, 0, 0, _WS_EX_DLGMODALFRAME, 0

Resizable Border

Stylebits #style.lb2, _WS_THICKFRAME, 0, 0, 0

Stylebits, Listboxes and Scrollbars

The default scrollbar lies on the right side of the listbox. When the listbox is tall enough to contain all the items, this scrollbar shows in the disabled state. Using the stylebits _WS_HSCROLL, _WS_VSCROLL, _WS_EX_LEFTSCROLLBAR, and _WS_EX_RIGHTSCROLLBAR, you can hide and even reposition the scroll bar.

Add Horizontal Scrollbar and Remove Vertical Scrollbar

Stylebits #style.lb1, _WS_HSCROLL, _WS_VSCROLL, 0, 0

Add Horizontal Position Scrollbar on Left

Stylebits #style.lb2, 0, 0, _WS_EX_LEFTSCROLLBAR, 0

Remove Default Disabled Listbar

Stylebits #style.lb3, 0, _LBS_DISABLENOSCROLL, 0, 0

Stylebits, Listboxes and Columns

The native listbox displays a single, scrolling column. The stylebits _LB_MULTICOLUMN allows the listbox to display more than one column. The number of columns is NOT determined by assigning a value. Rather, the number of columns is determined by the height of the listbox. Only vertically visible items are assigned to each column. By carefully controlling the height of the listbox, thus controlling the number of vertically visible listed items, the programmer can control the number of columns. The width of the column has no effect upon the number of columns. If not all columns will be visible, assign the stylebits _WS_HSCROLL as well.

Multicolumn Listbox with a Horizontal Scrollbar

Stylebits #style.lb3, _LBS_MULTICOLUMN or _WS_HSCROLL, 0,0, 0

Stylebits, Listboxes and Selection

A selection in the native listbox is chosen by double left - clicking. This highlights that item. Only one selection can in the selected state. That selection is retrieved using either the selection? or the selectionindex? command. Stylebits allow more than one item to be in the selected state at any given time.

Multiple Single Selections - String selection is toggled each time the user clicks or double-clicks the string. Any number of strings can be selected.

Stylebits #style.lb2, _LBS_MULTIPLESEL, 0, 0, 0

Extended Selection - The user can select multiple items using the SHIFT key and the mouse or special key combinations. Ctrl - Mouse Click selects multiple single items. Shift - Mouse Click selects a range of items.

Stylebits #style.lb3, _LBS_EXTENDEDSEL, 0, 0, 0

No Selection - Specifies that the list box contains items that can be viewed but not selected.

Stylebits #style.lb1, _LBS_NOSEL, 0, 0, 0

Stylebits and API Calls

Stylebits do allow multiple and extended selections in listboxes, but an API call (SendMessageA) is required to retrieve those selections. _LB_SETSEL is used to select one, multiple, or all items. By changing the state parameter, _LB_SETSEL is also used to clear one, multiple, or all items. The number of selected items is obtained with _LB_GETSELCOUNT. Because the Listbox sees the items beginning with 0, it may be necessary to decrement the item number by 1. The following demo is an enhancement of code originally issued by Bill Beasley in response to a [question] at the Liberty BASIC Forum.

'Setting Multiple Selections and Getting Multiple Selections
'with SendMessageA API calls
'A special Thank You to Bill Beasley for getting this code started

    Dim itemArray$(20)
    For i = 1 to 20
        itemArray$(i) = "Item Number ";Space$(i < 10);Str$(i)
    Next i
    nullParameter = 0 'Used as filler when parameter irrelevant

    Nomainwin
    WindowWidth = 400
    WindowHeight = 360

    UpperLeftX = Int((DisplayWidth - WindowWidth)/2)
    UpperLeftY = Int((DisplayHeight - WindowHeight)/2)

    Listbox #main.lb, itemArray$(), [selectItem], 20, 20, 140, 200
    '_LBS_MULTIPLESEL = Multiple Single Selections using Left Click
    Stylebits #main.lb, _LBS_MULTIPLESEL, 0, 0, 0
    '_LBS_EXTENDEDSEL = Multiple Single Selections using Ctrl - Left Click as well as
    'selection of a range of items using Shift - Left Click
    'Stylebits #main.lb, _LBS_EXTENDEDSEL, 0, 0, 0
    Statictext #main.st, "", 20, 230, 350, 100
    Button #main.b1, "Count Items Selected", [nItemsSelected], UL, 190, 40, 170, 32
    Button #main.b2, "List Items Selected", [listItemsSelected], UL, 190, 85, 170, 32
    Button #main.b3, "Select All Items", [selectAllItems], UL, 190, 130, 170, 32
    Button #main.b4, "Clear All Items", [clearAllItems], UL, 190, 175, 170, 32

    Open "Listbox Multi Select" for Window as #main
    #main, "Trapclose [endDemo]"
    #main, "Font Times_New_Roman 12 Bold"
    hLB = hwnd(#main.lb)

'Select some items
    For i = 1 to 20 step 3
        null = SendMessageA(hLB, _LB_SETSEL, 1, i)
    Next i
    #main.st, "Every 3rd Item is Selected"

    Wait

[selectItem]
'No action necessary
    Wait

[nItemsSelected]
    nItemsSelected = SendMessageA(hLB, _LB_GETSELCOUNT, nullParameter, nullParameter) 'no relevance (par1), (par2)
    #main.st, "There are ";nItemsSelected;" items selected."
    Wait

[listItemsSelected]
    itemsSel$ = ""
    For i = 1 to 20
        isSel = SendMessageA(hLB, _LB_GETSEL, i - 1, nullParameter) 'i - 1 = Item Number (par1), no relevance (par2)
        If isSel Then
            itemsSel$ = itemsSel$;itemArray$(i);", "
        End If
    Next i
    itemsSel$ = Left$(itemsSel$, Len(itemsSel$) - 2)
    #main.st, itemsSel$
    Wait

[selectAllItems]
    allSel = SendMessageA(hLB, _LB_SETSEL, 1, -1) ' 1 (par1) = Flag to Set, -1 (par2) = ALL
    Wait

[clearAllItems]
    allClear = SendMessageA(hLB, _LB_SETSEL, 0, -1) '0 (par1) = Flag to Clear, -1 (par2) = ALL
    Wait

[endDemo]
    Close #main
    End

Function SendMessageA(hW, msg, par1, par2)
    CallDLL #user32, "SendMessageA",_
        hW as Long, _ 'Handle of the control (listbox)
        msg as Long, _ 'Stylebits (Windows Constant)
        par1 as Long, _ 'Parameter 1 (sometimes irrelevant)
        par2 as Long, _ 'Parameter 2 (sometimes irrelevant)
        SendMessageA as long 'Return Value, 1 = success
    End Function

The Stylebits Corner Series

What else can be done with stylebits? In the coming months the STYLEBITS command will be used to, among other things,


Eager to try Stylebits with buttons and other window controls? Can't wait for the next newsletter? Good news! The best way to explore and assign stylebit commands is with the Stylebits Wizard option found in Alyce Watson's [Liberty BASIC Workshop].


DEMO

The file stylebitsDemo6.bas is included in the zipped archive of this newsletter.


Home

LB Wire Frame Lib

Stylebits - Listboxes

IExpress

Prompt

Icons

Screen Capture

Eddie's Lessons

Combobox

Scrolling Texteditor

Nested If/Then

Sprite Byte

Eddie

Moving Listbox Items

Newsletter help

Submissions

Index